package com.xhblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhblog.entity.Article;
import com.xhblog.entity.dto.PageDto;
import com.xhblog.entity.vo.ArticleArchivesVo;
import com.xhblog.entity.vo.ArticleVo;
import com.xhblog.mapper.ArticleMapper;
import com.xhblog.result.Result;
import com.xhblog.service.ArticleService;
import com.xhblog.service.SysUserService;
import com.xhblog.service.TagService;
import lombok.extern.log4j.Log4j2;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pengyouhong
 * create 2022/5/17 18:11
 */
@Service
@Log4j2
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private TagService tagService;

    @Override
    public Result listArticle(PageDto pageDto) {
        Page<Article> page = new Page<>(pageDto.getPage(), pageDto.getPageSize());

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();

        /*
         * 根据是否置顶和创建时间排序并分页
         * */

        //是否置顶，order by create_date desc
        queryWrapper.orderByDesc(Article::getWeight, Article::getCreateDate);


        Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);

        List<Article> articleList = articlePage.getRecords();


        //将集合里的实体类对象转换成vo对象
        List<ArticleVo> articleVoList = copyList(articleList, true, true);

        articleVoList.forEach(System.out::println);

        return Result.success(articleVoList);
    }

    @Override
    public Result hotArticle(int limit) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();


        /*
         * select id,title from tag order by viewcounts desc limit 5
         * */
        articleLambdaQueryWrapper.orderByDesc(Article::getViewCounts);
        articleLambdaQueryWrapper.select(Article::getId, Article::getTitle);
        articleLambdaQueryWrapper.last("limit " + limit);

        List<Article> articleList = articleMapper.selectList(articleLambdaQueryWrapper);

        return Result.success(copyList(articleList, false, false));
    }

    @Override
    public Result newArticle(int limit) {
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        /*
         * select id,title from ms_article order by create_date desc limit 5
         * */
        articleLambdaQueryWrapper.orderByDesc(Article::getCreateDate);
        articleLambdaQueryWrapper.select(Article::getId, Article::getTitle, Article::getCreateDate);
        articleLambdaQueryWrapper.last("limit " + limit);

        List<Article> articleList = articleMapper.selectList(articleLambdaQueryWrapper);

        return Result.success(copyList(articleList, false, false));
    }

    @Override
    public Result listArchives() {
        List<ArticleArchivesVo> articleArchivesVoList = articleMapper.listArchives();

        return Result.success(articleArchivesVoList);
    }


    /**
     * 将实体类集合转换vo对象集合
     *
     * @param articleList 文章集合
     * @return 返回文章vo集合
     */
    private List<ArticleVo> copyList(List<Article> articleList, boolean isTag, boolean isAuthor) {
        List<ArticleVo> articleVoList = new ArrayList<>();

        //遍历集合，将每个对象转换问vo对象并添加到vo对象集合
        articleList.forEach(article -> articleVoList.add(copy(article, isTag, isAuthor)));
        return articleVoList;
    }

    /**
     * @param article 文章实体类对象
     * @return 将文章实体类对象转换为文章vo对象
     */
    private ArticleVo copy(Article article, boolean isTag, boolean isAuthor) {

        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article, articleVo);

        articleVo.setId(article.getId().toString());


        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));

        if (isTag) {
            articleVo.setTags(tagService.findTagByArticleId(article.getId()));
        }

        if (isAuthor) {
            String nickname = sysUserService.getById(article.getAuthorId()).getNickname();
            articleVo.setAuthor(nickname);
        }
        return articleVo;
    }
}
